// 设备类型定义
export type DeviceType = 'mobile' | 'browser' | 'desktop';

export interface DeviceCapabilities {
  canReceiveText: boolean;
  canSendVoice: boolean;
  canGenerateQR: boolean;
  canScanQR: boolean;
}

export interface DeviceInfo {
  id: string;
  type: DeviceType;
  name: string;
  capabilities: DeviceCapabilities;
  userAgent?: string;
  platform?: string;
  timestamp: number;
  hardwareFingerprint?: string; // 新增字段
}

export interface RegisteredDevice extends DeviceInfo {
  socketId: string;
  lastSeen: number;
  isOnline: boolean;
}

// 配对相关类型
export interface PairingCode {
  code: string;
  deviceId: string;
  expiresAt: number;
  isUsed: boolean;
}

export interface PairingRequest {
  code: string;
  targetDeviceId: string;
  requestingDeviceId: string;
}

export interface PairingData {
  success: boolean;
  deviceId: string;
  deviceName: string;
  message?: string;
}

// 语音输入相关类型
export interface VoiceTextData {
  text: string;
  confidence: number;
  language: string;
  timestamp: number;
  deviceId: string;
  isFinal: boolean;
}

export interface VoiceStartData {
  language: string;
  continuous: boolean;
  interimResults: boolean;
}

// Socket.io 事件类型定义
export interface ServerToClientEvents {
  // 设备管理
  'device:registered': (data: { success: boolean; deviceId: string; message?: string }) => void;
  'device:list': (devices: DeviceInfo[]) => void;
  'device:status-changed': (data: { deviceId: string; isOnline: boolean }) => void;
  
  // 配对相关
  'pairing:code-generated': (data: { code: string; qrCodeUrl: string; expiresAt: number }) => void;
  'pairing:request': (data: PairingRequest) => void;
  'pairing:success': (data: PairingData) => void;
  'pairing:failed': (data: { message: string; code?: string }) => void;
  'device:pair:result': (data: { success: boolean; deviceId?: string; deviceName?: string; error?: string }) => void;
  'device:paired': (data: { success: boolean; deviceId: string; deviceName: string }) => void;
  'device:pairing-status': (data: { success: boolean; deviceId?: string; pairings?: any[]; error?: string }) => void;
  
  // 语音输入
  'voice:text': (data: VoiceTextData) => void;
  'voice:start': () => void;
  'voice:stop': () => void;
  'voice:error': (data: { message: string; code?: string }) => void;
  'voice:result': (data: { success: boolean; text?: string; confidence?: number; provider?: string; language?: string; processingTime?: number; timestamp: number; error?: string }) => void;
  
  // 系统消息
  'system:message': (data: { type: 'info' | 'warning' | 'error'; message: string }) => void;
  'connection:status': (data: { connected: boolean; timestamp: number }) => void;
}

export interface ClientToServerEvents {
  // 设备管理
  'device:register': (deviceInfo: Omit<DeviceInfo, 'id' | 'timestamp'>) => void;
  'device:get-list': () => void;
  'device:ping': () => void;
  
  // 配对相关
  'pairing:generate-code': () => void;
  'pairing:use-code': (data: { code: string }) => void;
  'pairing:accept': (data: { requestingDeviceId: string }) => void;
  'pairing:reject': (data: { requestingDeviceId: string; reason?: string }) => void;
  'device:pair': (data: { pairingCode: string }) => void;
  'device:get-pairing-status': (data: { deviceId: string }) => void;
  
  // 语音输入
  'voice:send-text': (data: Omit<VoiceTextData, 'timestamp'>) => void;
  'voice:start-session': (data: VoiceStartData) => void;
  'voice:end-session': () => void;
  'voice:recognize': (data: { audioData: string; language: string; timestamp: number; options?: { provider?: string; format?: string } }) => void;
  // 新增流式语音识别事件
  'voice:stream/start': (params?: any) => void;
  'voice:stream/data': (audioChunk: any) => void;
  'voice:stream/end': () => void;
  
  // 系统
  'system:heartbeat': () => void;
}

export interface InterServerEvents {
  ping: () => void;
}

export interface SocketData {
  deviceId?: string;
  deviceType?: DeviceType;
  deviceName?: string;
  pairedDevices?: string[];
  lastActivity?: number;
}

// 服务器配置类型
export interface ServerConfig {
  port: number;
  host: string;
  cors: {
    origin: string | string[];
    credentials: boolean;
  };
  rateLimit: {
    windowMs: number;
    max: number;
  };
  pairing: {
    codeLength: number;
    expirationTime: number; // 毫秒
    maxAttempts: number;
    offlineTimeout: number; // 60秒宽限期
  };
  cleanup: {
    deviceTimeout: number; // 毫秒
    pairingCodeCleanup: number; // 毫秒
  };
}

// 错误类型
export class SmartInputError extends Error {
  constructor(
    message: string,
    public code: string,
    public statusCode: number = 500
  ) {
    super(message);
    this.name = 'SmartInputError';
  }
}

export interface ErrorResponse {
  success: false;
  error: {
    code: string;
    message: string;
    timestamp: number;
  };
}

export interface SuccessResponse<T = any> {
  success: true;
  data: T;
  timestamp: number;
}

export type ApiResponse<T = any> = SuccessResponse<T> | ErrorResponse;

// 统计和监控类型
export interface ServerStats {
  uptime: number;
  connectedDevices: number;
  totalConnections: number;
  messagesProcessed: number;
  pairingAttempts: number;
  successfulPairings: number;
  errors: number;
  lastActivity: number;
}

export interface DeviceStats {
  deviceId: string;
  connectTime: number;
  messagesReceived: number;
  messagesSent: number;
  pairingAttempts: number;
  lastActivity: number;
}

// 日志类型
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';

export interface LogEntry {
  level: LogLevel;
  message: string;
  timestamp: number;
  deviceId?: string;
  socketId?: string;
  metadata?: Record<string, any> | undefined;
}

// 中间件类型
export interface AuthenticatedSocket extends Socket<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData> {
  deviceId: string;
  deviceInfo: RegisteredDevice;
}

// 导入Socket.io类型
import { Socket } from 'socket.io';
